import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },

  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "Home" */ '@/views/Home.vue'),
    meta: { title: '主页' }
  },

  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "About" */ '@/views/About.vue'),
    meta: { title: '我的' }
  },

  {
    path: '/standingBook', // 设备台账
    name: 'standingBook',
    component: () => import(/* webpackChunkName: "standingBook" */ '@/views/standingBook/index.vue')
  },

  {
    path: '/standingBook-detail',
    name: 'standingBook-detail',
    component: () => import(/* webpackChunkName: "standingBook-detail" */ '@/views/standingBook/detail.vue')
  },

  {
    path: '/standingBook-guarantee',
    name: 'standingBook-guarantee',
    component: () => import(/* webpackChunkName: "standingBook-guarantee" */ '@/views/standingBook/guarantee.vue')
  },

  {
    path: '/maintain', // 设备维保
    name: 'maintain',
    component: () => import(/* webpackChunkName: "maintain" */ '@/views/maintain/index.vue')
  },

  {
    path: '/polling', // 巡检管理
    name: 'polling',
    component: () => import(/* webpackChunkName: "polling" */ '@/views/polling/index.vue')
  },
  {
    path: '/pollingForm', // 巡检表单
    name: 'pollingForm',
    component: () => import(/* webpackChunkName: "polling" */ '@/views/polling/pollingForm.vue')
  },

  {
    path: '/polling-detail',
    name: 'polling-detail',
    component: () => import(/* webpackChunkName: "polling-detail" */ '@/views/polling/detai.vue')
  },

  {
    path: '/electronic', // 电子巡更
    name: 'electronic',
    component: () => import(/* webpackChunkName: "electronic" */ '@/views/electronic/index.vue')
  },
  {
    path: '/electronicDetail', // 电子巡更打卡
    name: 'electronicDetail',
    component: () => import(/* webpackChunkName: "electronic" */ '@/views/electronic/electronicDetail.vue')
  },
  {
    path: '/reportform', // 报表查看
    name: 'reportform',
    component: () => import(/* webpackChunkName: "reportfrom" */ '@/views/reportfrom/index.vue')
  },
  {
    path: '/mokePage', // 假页面
    name: 'mokePage',
    component: () => import(/* webpackChunkName: "mokePage" */ '@/views/reportfrom/mokePage.vue')
  },
  {
    path: '/repairForm', // 维修记录填写
    name: 'repairForm',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/reportfrom/secondForm/repairForm')
  },
  {
    path: '/maintainForm', // 维保记录填写
    name: 'maintainForm',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/reportfrom/secondForm/maintainForm')
  },
  {
    path: '/inspectionForm', // 巡检记录填写
    name: 'inspectionForm',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/reportfrom/secondForm/inspectionForm')
  }, {
    path: '/patrolForm', // 巡更记录填写
    name: 'patrolForm',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/reportfrom/secondForm/patrolForm')
  },
  // {
  //   path: '/patrolForm', // 巡更记录填写
  //   name: 'patrolForm',
  //   component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/reportfrom/secondForm/patrolForm')
  // },

  {
    path: '/taskhall',
    name: 'taskhall',
    component: () => import(/* webpackChunkName: "taskhall" */ '@/views/taskhall/index.vue')
  },

  {
    path: '/repairsContent', // 填写报修
    name: 'repairsContent',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/standingBook/repairsContent')
  },

  {
    path: '/maintainContent', // 填写维修
    name: 'maintainContent',
    component: () => import(/* webpackChunkName: "maintainContent" */ '@/views/standingBook/maintainContent')
  },

  {
    path: '/service', // 填写维修
    name: 'service',
    component: () => import(/* webpackChunkName: "service" */ '@/views/service/index')
  },
  // flutter 首页点击任务跳转页面
  {
    path: '/form', // 填写维修
    name: 'form',
    component: () => import(/* webpackChunkName: "service" */ '@/views/form')
  },
  {
    path: '/serviceContent', // 填写维修
    name: 'serviceContent',
    component: () => import(/* webpackChunkName: "serviceContent" */ '@/views/service/serviceContent')
  },
  {
    path: '/statistics', // 统计页面
    name: 'Statistics',
    component: () => import(/* webpackChunkName: "serviceContent" */ '@/views/statistics')
  },
  {
    path: '/reportStatistics', // 报表统计页面---（10。07要求添加）
    name: 'reportStatistics',
    component: () => import(/* webpackChunkName: "serviceContent" */ '@/views/reportStatistics')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(async (to, form, next) => {
  const { token, tenantId, personId } = to.query

  if (token) {
    window.localStorage.setItem('token', token)
  }

  if (tenantId) {
    window.localStorage.setItem('tenantId', tenantId)
  }

  if (personId) {
    window.localStorage.setItem('personId', personId)
  }

  if (
    window.localStorage.getItem('token') &&
    window.localStorage.getItem('tenantId') &&
    window.localStorage.getItem('personId')
  ) {
    next()
  }

  // next( )

  /*  // // 进入页面时，检测是否有 Token ，有 Token 就存起来
  const { token } = to.query
  if (token) {
    store.commit('user/SET_TOKEN', token)
  }

  const { token: _token, userInfo } = store.getters

  // 有 token 则获取用户信息，否则退出登录
  if (_token) {
    // 判断是否有用户信息，有则 next，没有则获取之后再 next
    const hasUserInfo = Object.keys(userInfo).length > 0
    if (hasUserInfo) {
      next()
    } else {
      try {
        await store.dispatch('user/getUesrInfo')
        next()
      } catch (e) {
        // TODO 退出登录
      }
    }
  } else {
    if (['/401'].includes(to.path)) {
      next()
    } else {
      // TODO 退出登录
    }
  } */
})

export default router
